<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="国家" prop="country">
        <el-input v-model="dataForm.country" placeholder="国家"></el-input>
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-input v-model="dataForm.province" placeholder="省"></el-input>
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input v-model="dataForm.city" placeholder="市"></el-input>
      </el-form-item>
      <el-form-item label="县城" prop="counties">
        <el-input v-model="dataForm.counties" placeholder="县城"></el-input>
      </el-form-item>
      <el-form-item label="详细地址" prop="dettailAdderss">
        <el-input v-model="dataForm.dettailAdderss" placeholder="详细地址"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createUtc">
        <el-input v-model="dataForm.createUtc" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="modifyUtc">
        <el-input v-model="dataForm.modifyUtc" placeholder="更新时间"></el-input>
      </el-form-item>
      <el-form-item label="启用状态(是否启用，可以用 0，1也可以使用“是”，“否”)" prop="status">
        <el-input v-model="dataForm.status" placeholder="启用状态(是否启用，可以用 0，1也可以使用“是”，“否”)"></el-input>
      </el-form-item>
      <el-form-item label="操作员" prop="operator">
        <el-input v-model="dataForm.operator" placeholder="操作员"></el-input>
      </el-form-item>
      <el-form-item label="是否删除(数字0：未插入，1.未删除,2：删除，)" prop="isDeleted">
        <el-input v-model="dataForm.isDeleted" placeholder="是否删除(数字0：未插入，1.未删除,2：删除，)"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          country: '',
          province: '',
          city: '',
          counties: '',
          dettailAdderss: '',
          createUtc: '',
          modifyUtc: '',
          status: '',
          operator: '',
          isDeleted: ''
        },
        dataRule: {
          country: [
            { required: true, message: '国家不能为空', trigger: 'blur' }
          ],
          province: [
            { required: true, message: '省不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '市不能为空', trigger: 'blur' }
          ],
          counties: [
            { required: true, message: '县城不能为空', trigger: 'blur' }
          ],
          dettailAdderss: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          createUtc: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifyUtc: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '启用状态(是否启用，可以用 0，1也可以使用“是”，“否”)不能为空', trigger: 'blur' }
          ],
          operator: [
            { required: true, message: '操作员不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除(数字0：未插入，1.未删除,2：删除，)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.upkgoodsaddress.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.country = data.upkgoodsaddress.country
                this.dataForm.province = data.upkgoodsaddress.province
                this.dataForm.city = data.upkgoodsaddress.city
                this.dataForm.counties = data.upkgoodsaddress.counties
                this.dataForm.dettailAdderss = data.upkgoodsaddress.dettailAdderss
                this.dataForm.createUtc = data.upkgoodsaddress.createUtc
                this.dataForm.modifyUtc = data.upkgoodsaddress.modifyUtc
                this.dataForm.status = data.upkgoodsaddress.status
                this.dataForm.operator = data.upkgoodsaddress.operator
                this.dataForm.isDeleted = data.upkgoodsaddress.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'country': this.dataForm.country,
              'province': this.dataForm.province,
              'city': this.dataForm.city,
              'counties': this.dataForm.counties,
              'dettailAdderss': this.dataForm.dettailAdderss,
              'createUtc': this.dataForm.createUtc,
              'modifyUtc': this.dataForm.modifyUtc,
              'status': this.dataForm.status,
              'operator': this.dataForm.operator,
              'isDeleted': this.dataForm.isDeleted
            }
            var tick = !this.dataForm.id ? API.upkgoodsaddress.add(params) : API.upkgoodsaddress.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
